Ajax = new Object();

Ajax.error = function(err) {
	console.log("Error : " + err);
	Ajax.showErr(err);
}

Ajax.showOk = function(msg) {
	$('.ajaxMsg').text(msg);
	window.setTimeout(function(){
							$('.ajaxMsg').text('');
						}, 2000);
}

Ajax.showErr = function(msg) {
	$('.ajaxErr').text(msg);
	window.setTimeout(function(){
							$('.ajaxErr').text('');
						}, 2000);
}

Ajax.persons = function() {
	
	Ajax.showOk('updating');
		
	jQuery.ajax({
		url: '/resource/persons',
		crossDomain: true,
		type: 'GET',
		dataType: 'json',
		cache: false,
		success: function(data) {
			
			var persons = Context.listPersons.persons;
			
			for(var key in data) {
				
				var item = data[key];
				var person = new Person();
				person.id = item.id;
				person.firstName = item.firstName;
				person.lastName = item.lastName;
				
				//Enrichissement
				persons[person.id] = person;
			}
			
			Ajax.showOk('updated');
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(persons));
			
			//Reload IHM
			Context.updateListPersons();
		},
		error: function(jqXHR, textStatus, errorThrown) {
			
			Ajax.error(textStatus +' '+ errorThrown);
			
			//Cache data
			var strPersons = localStorage.getItem('persons');
			Context.listPersons.persons = jQuery.parseJSON(strPersons);
			
			//Reload IHM
			Context.updateListPersons();
		}
	});	

}

Ajax.addPerson = function(person) {

	jQuery.ajax({
		url: '/resource/persons',
		crossDomain: true,
		data: {id: person.id, firstName: person.firstName, lastName: person.lastName},
		type: 'POST',
		cache: false,
		success: function(data) {
			
			Ajax.showOk('person added');
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
		}
	});	
}

Ajax.removePerson = function(id) {
	
	jQuery.ajax({
		url: '/resource/person/'+id,
		crossDomain: true,
		type: 'DELETE',
		cache: false,
		success: function(data) {
			
			Ajax.showOk('person removed');
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
		}
	});	
	
}

Ajax.tasks = function() {
	
	Ajax.showOk('updating');
	
	jQuery.ajax({
		url: '/resource/person/'+Context.currentPerson.id+'/tasks',
		type: 'GET',
		dataType: 'json',
		cache: false,
		success: function(data) {
			
			var tasks = Context.currentPerson.tasks;
			for(var key in data) {
				
				var item = data[key];
				var task = new Task();
				task.id = item.id;
				task.label = item.label;
				task.description = item.description;
				
				//Enrichissement
				tasks[task.id] = task;
			}
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
			
			//Reload IHM
			Context.updateListTasks();
			
			Ajax.showOk('updated');
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
			
			//Cache data
			var strPersons = localStorage.getItem('persons');
			var personsCache = jQuery.parseJSON(strPersons);
			
			if(personsCache[Context.currentPerson.id]) {
				Context.currentPerson.tasks = personsCache[Context.currentPerson.id].tasks;
			}
			
			//Reload IHM
			Context.updateListTasks();
		}
	});	

}

Ajax.addTask = function(task) {

	jQuery.ajax({
		url: '/resource/tasks',
		data: {id: task.id, ownerId: Context.currentPerson.id, label: task.label, description: task.description},
		type: 'POST',
		cache: false,
		success: function(data) {

			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
			
			Ajax.showOk('task added');
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
		}
	});

}

Ajax.updateTask = function(task) {

	jQuery.ajax({
		url: '/resource/task/'+task.id,
		data: {label: task.label, description: task.description},
		type: 'POST',
		cache: false,
		success: function(data) {
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
			
			Ajax.showOk('task updated');
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
		}
	});	
}

Ajax.closeTask = function(id) {
	jQuery.ajax({
		url: '/resource/task/'+id,
		type: 'DELETE',
		dataType: 'json',
		cache: false,
		success: function(data) {
			
			//Cache data
			localStorage.setItem('persons', JSON.stringify(Context.listPersons.persons));
			
			Ajax.showOk('task removed');
		},
		error: function(jqXHR, textStatus, errorThrown) {
			Ajax.error(errorThrown);
		}
	});	
}